define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'huifang/index' + location.search,
                    add_url: 'huifang/add',
                    edit_url: 'huifang/edit',
                    del_url: 'huifang/del',
                    multi_url: 'huifang/multi',
                    import_url: 'huifang/import',
                    table: 'huifang',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //隐藏快速搜索框
                search: false,
                //默认打开通用搜索栏
                searchFormVisible: true,
                //指定搜索模板
                searchFormTemplate: 'huifangformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'bhid', title: __('Bhid'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {
                            field: 'sex',
                            title: __('Sex'),
                            searchList: {"male": __('Sex male'), "female": __('Sex female')},
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'age', title: __('Age'), operate: false},
                        {field: 'type', title: __('Type'), operate: 'LIKE', operate: false},
                        {field: 'area', title: __('Area'), operate: 'LIKE', operate: false},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {
                            field: 'intime',
                            title: __('Intime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.dateonly
                        },
                        {
                            field: 'outtime',
                            title: __('Outtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.dateonly
                        },
                        {
                            field: 'yytime',
                            title: __('Yytime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.dateonly
                        },
                        {
                            field: 'hftime',
                            title: __('Hftime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.dateonly
                        },
                        {
                            field: 'admin.nickname',
                            title: __('Admin.nickname'),
                            operate: 'LIKE',
                            autocomplete: false,
                            searchList: $.getJSON("huifang/getAdminNickname"),
                            visible: function (row) {
                                //返回true时按钮显示,返回false隐藏
                                return true;
                            }
                        },
                        {
                            field: 'huifang',
                            width: "120px",
                            operate: false,
                            title: __('Huifang'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'huifangjilu',
                                    text: __('Huifangjilu'),
                                    title: __('Huifangjilu'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'huifang/huifangjilu',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'huifangadd',
                                    text: __('Huifangadd'),
                                    title: __('Huifangadd'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: 'huifang/huifangadd',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值, 今天需要回访的人数和明天需要回访的人数
                $("#today_num").text(data.extend.today_num);
                $("#tomorrow_num").text(data.extend.tomorrow_num);

                //判断是否隐藏 "导医" 项目
                if(data.extend.showNickname == '0'){
                    table.bootstrapTable('hideColumn', 'admin.nickname');
                }

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'huifang/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'huifang/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'huifang/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        huifangjilu: function () {
            Controller.api.bindevent();
        },
        huifangadd: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});